#ifndef DklHeader_h
#define DklHeader_h

// v0 2006-09-15 by alex.kaplan@web.de

#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#ifndef WIN32
#  include <unistd.h>
#endif
#include "IOError.h"
#include "dackel_date.h"

class DklHeader {
 public:
		static const int COMMENT_LENGTH=256;

		u_int8_t    magic_byte;        //identifies file in dkl format
		u_int8_t    version;           //dkl format version
		u_int32_t   hdr_length;        //total length of header in bytes
		dackel_date date;              //date when this dkl file was created
		u_int32_t   ev_count;       //number of total events in this file

		u_int16_t   modus;             //16-bit flags register describing
		                               //modus of the measurement
		                               //(shutter status etc.)

    //comment in ascii text, zero terminated
		char        comment[COMMENT_LENGTH];

		u_int8_t    sfs_length;        //length of spin flip schema
    u_int8_t    sf_loops;          //number of schema repeats
		u_int32_t   fcID;              //ID of first cycle

		u_int32_t   cycle_count;       //total number of cycles in this file
                                   //(if there were no errors during the
                                   // measurment and no interrupts, it must
		                               // be cycle_count = sfs_length * sf_loops

		u_int8_t    fileOK;            //this is zero if measurment was succesful
		                               //(no errors and no interrupts)

    u_int8_t    noc;               //number of counters
		                               //that are read after each cycle

    DklHeader(u_int16_t modus,const char *comment, u_int8_t sfs_length,
							u_int8_t sf_loops, u_int32_t fcID, u_int8_t noc );

		DklHeader( int fhandle ); //constructor
                              //reads data from fhandle
		                          //fhandle must be an open file
		                          //handle given by posix open()

		void fromFile( int fhandle );  //reads data from fhandle
		                               //fhandle must be an open file
		                               //handle given by posix open()

		void toFile( int fhandle );    //writes data to fhandle
		                               //fhandle must be an open file
		                               //handle given by posix open()

    //returns the value for specific counter and cycle
    //cycle_no is counted from zero
		//relative to the first cycle in the dkl file
		u_int32_t getCounter(u_int32_t counter_no, u_int32_t cycle_no);

    //sets the value for specific counter and cycle
    //cycle_no is counted from zero
		//relative to the first cycle in the dkl file
    void setCounter(u_int32_t counter_no, u_int32_t cycle_no, u_int32_t value);

		//returns c-string containing header info
		const char *infoString();

		private:
		//access the following way: counter_data[cycle_no * noc + counter_no]
    //better: via getCounter() and setCounter()
    
		u_int32_t  cd_size;
		u_int32_t  *counterdata;

};

#endif
